package com.gaattc.dao;

import com.gaattc.model.User;
import com.gaattc.model.bo.AdminLoginBO;
import com.gaattc.model.bo.UserChangePwdBO;
import com.gaattc.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class UserDaoImpl implements UserDao {
    @Override
    public List<User> allUser() {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<User> list = null;
        try {
            list = queryRunner.query("select * from user", new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int deleteUser(int id) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        try {
            return queryRunner.update("delete from user where id = ?", id);
        } catch (SQLException e) {
            e.printStackTrace();
            return 10000;
        }
    }

    @Override
    public List<User> searchUser(String word) {
        QueryRunner queryRunner = new QueryRunner(DruidUtils.getDataSource());
        List<User> list = null;
        String name = "%" + word + "%";
        try {
            list = queryRunner.query("select * from user where nickname like ?",
                    new BeanListHandler<User>(User.class), name);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int login(AdminLoginBO loginBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        Long query = null;
        try {
            query = (Long) runner.query("select count(id) from user where email = ? and pwd = ?",
                    new ScalarHandler(), loginBO.getEmail(), loginBO.getPwd());
        } catch (SQLException e) {
            return 500;
        }
        return query != 0 ? 200 : 404;
    }

    @Override
    public int signUp(User user) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("insert into user values (null,?,?,?,?,?,?)",
                    user.getEmail(),user.getNickname(),user.getPwd(),user.getRecipient(),user.getPhone(),user.getAddress());
        } catch (SQLException e) {
            e.printStackTrace();
            return 10000;
        }
        return 0;
    }

    @Override
    public User data(String token) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        User user = null;
        try {
            user = runner.query("select * from user where email = ?", new BeanHandler<>(User.class), token);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    @Override
    public int updatePwd(UserChangePwdBO userChangePwdBO) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            Long exist = (Long) runner.query("select count(*) from user where id = ? and pwd = ?", new ScalarHandler(),
                    userChangePwdBO.getId(), userChangePwdBO.getOldPwd());
            if(exist.intValue() == 0) {
                return -1;
            }
            runner.update("update user set pwd = ? where id = ?", userChangePwdBO.getNewPwd(), userChangePwdBO.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public void updateUserData(User user) {
        QueryRunner runner = new QueryRunner(DruidUtils.getDataSource());
        try {
            runner.update("update user set nickname = ?, recipient = ?, address = ?, phone = ? where id = ?",
                    user.getNickname(), user.getRecipient(), user.getAddress(), user.getPhone(), user.getId());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
